from urllib.request import urlopen, Request

if __name__ == '__main__':
    # 要获取页面的网址
    url = "https://www.baidu.com"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
    }

    # 自定义请求头
    res = Request(url=url, headers=headers)
    # 使用请求对象发送请求
    resp = urlopen(res)
    print(type(resp))
    # 获取 HTTP 响应状态码
    print(resp.getcode())

    # 读响应内容
    content = resp.read().decode("utf-8")
    print(content)